AltClickInvite = LibStub("AceAddon-3.0"):NewAddon("AltClickInvite", "AceConsole-3.0")

local options = {
    name = "AltClickInvite",
    handler = AltClickInvite,
    type = "group",
    args = {
        toggle = {
            type = "toggle",
            name = "Enable",
            desc = "Enable/Disable AltClickInvite",
            get = "GetToggle",
            set = "Toggle",
        },
    },
}

local defaults = {
    profile = {
        toggle = true,
    },
}

--AltClickInvite.toggle = true;

function AltClickInvite:OnInitialize()
	Old_SetItemRef = SetItemRef;
	AltClickInvite:Print("AltClickInvite Initialized");
	
	function SetItemRef(link, ...)
		if (not IsAltKeyDown()) or (not self.db.profile.toggle) then
			Old_SetItemRef(link, ...);
		else
			if ( strsub(link, 1, 6) == "player" ) then
				local namelink = strsub(link, 8);
				local name, lineid = strsplit(":", namelink);
				if ( name and (strlen(name) > 0) ) then
					local begin = string.find(name, "%s[^%s]+$");
					if ( begin ) then
						name = strsub(name, begin+1);
					end
					InviteUnit(name);
				end
			end
		end
	end
	
	self.db = LibStub("AceDB-3.0"):New("ACIDB", defaults, "Default");
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("AltClickInvite", options, {"AltClickInvite", "aci"});
--	LibStub("AceConsole-3.0"):RegisterChatCommand("AltClickInvite", "ChatCommand");
--	self:RegisterChatCommand("aci", "ChatCommand");
end

--[[function AltClickInvite:ChatCommand(input)
    if not input or input:trim() == "" then
--  	  LibStub("AceConfigCmd-3.0").HandleCommand(AltClickInvite, "aci", "AltClickInvite", "toggle");
		AltClickInvite:Print("No Input");
    else
 --       LibStub("AceConfigCmd-3.0").HandleCommand(AltClickInvite, "aci", "AltClickInvite", input);
		AltClickInvite:Print("Input");
    end
end]]

function AltClickInvite:GetToggle(info)
	return self.db.profile.toggle;
end

function AltClickInvite:Toggle(info, value)
	self.db.profile.toggle = value;
	if self.db.profile.toggle then
		AltClickInvite:Print("AltClickInvite Enabled");
	else
		AltClickInvite:Print("AltClickInvite Disabled");
	end
end